<?php
/**
 * User: MT
 * Date: 2018/5/5 0005
 */
namespace core;
use core\lib\log;

class core{
    static public $classMap = array();
    public $assign = [];
    static public function run(){
        $route = new \core\lib\route();
        $action=  $route->action;
        $method = $route->method;
        //开启日志
        \core\lib\log::init();
        //加载控制器
        $act_file = APP.'/controller/'.$action.'Controller.php';
        if(is_file($act_file)){
            include $act_file;
            $obj = "\\".MODULE."\\controller\\".ucfirst($action).'Controller';
            $action_obj = new $obj();
            //日志
            log::log($route->action.' '.$route->method);
            $action_obj->$method();
        }else{
            pp('没有找到控制器'.$action);
        }
    }
    static public function load($class){
        if(isset($classMap[$class])){
            return ture;
        }else{
            $class = str_replace('\\','/',$class);
            $file = ROOT.'/'.$class.'.php';
            if(is_file($file)){
                include $file;
                self::$classMap[$class] = $class;
            }else{
                return false;
            }
        }
    }
    public function assign($name,$value){
        $this->assign[$name] = $value;
    }
    public function display($html){
        $file = APP."/view/".$html.'.html';
        if(is_file($file)){
            //\Twig_Autoloader::register();
            $loader = new \Twig_Loader_Filesystem(APP.'/view');
            $twig = new \Twig_Environment($loader,[
                'cache' => ROOT.'/runtime/cache',
                'debug' => APP_DEBUG
            ]);
            //$template = $twig->loadTemplate($html);
            $template = $twig->load($html.'.html');
            $template->display($this->assign?$this->assign:'');
        }
    }
}